#import <UIKit/UIkit.h>
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
#import <CoreBluetooth/CoreBluetooth.h>

#define UUID(x)		[CBUUID UUIDWithString:(x)]

@class BLEDevice;
@class BLEManager;

typedef NS_ENUM(int, BLEManagerState) {
	BLEManagerStatePoweredOff		,
	BLEManagerStatePoweredOn		,
};

//=============================================================
@protocol BLEDelegate <NSObject>

@optional
- (void)manager:(BLEManager *)manager didUpdateState:(BLEManagerState)state;	//蓝牙状态已变化
- (void)manager:(BLEManager *)manager willScanWithServices:(NSArray <CBUUID *> *)services;		//将要扫描（通过此方法可定制需要的UUID）

- (id)manager:(BLEManager *)manager needAnalyzeDevice:(BLEDevice *)device;		//当管理器不可解析当前设备时，会向代理请求解析，返回用户解析后设备

- (BOOL)manager:(BLEManager *)manager shouldDiscoverDevice:(BLEDevice *)device;	//将要发现（添加）新设备，代理可通过返回NO来拒绝添加当前设备
- (void)manager:(BLEManager *)manager didDiscoverDevice:(BLEDevice *)device;	//发现新设备

- (void)manager:(BLEManager *)manager didRemoveDevice:(BLEDevice *)device atIndex:(NSUInteger)index;	//设备已移除

- (void)manager:(BLEManager *)manager didRecentDevice:(BLEDevice *)device;		//最近的设备已更新
@end

/*=============================================================
 蓝牙管理类
 ============================================================*/
@interface BLEManager : NSObject <CBCentralManagerDelegate, CBPeripheralDelegate>

+ (instancetype)common;		//返回通用控制器（不会主动创建）
+ (instancetype)commonWithDelegate:(id <BLEDelegate>)delegate;		//返回通用管理器（会对蓝牙状态进行处理）

+ (instancetype)health;		//返回健康控制器（不会主动创建）
+ (instancetype)healthWithDelegate:(id <BLEDelegate>)delegate;		//返回健康管理器（会对蓝牙状态进行处理）

- (instancetype)init	NS_UNAVAILABLE;
- (instancetype)initWithDelegate:(id <BLEDelegate>)delegate;

@property (nonatomic, assign) id<BLEDelegate>		delegate;

@property (nonatomic, readonly) CBCentralManager	*manager;
@property (nonatomic, readonly) BOOL				scaning;		//扫描中
@property (nonatomic, readonly) BLEManagerState		state;			//管理器当前状态

@property (nonatomic, readonly) NSMutableArray <CBUUID *>		*services;		//当前需要过滤的服务
@property (nonatomic, readonly) NSMutableArray <BLEDevice *>	*devices;		//当前管理器中的设备

@property (nonatomic, assign)	BOOL				allowRemove;	//允许管理器移除设备（默认 NO， 设为YES时，当检测到设备信号过低时，管理器移除设备并委托代理处理）

- (BOOL)startScan;		//扫描外设（蓝牙未开启时返回NO）
- (void)stopScan;		//停止扫描

- (void)disconnect;		//断开现有设备的连接并从devices中移除

//在使用到蓝牙管理器的页面退出时要执行清理
- (void)clean;			//清理设备（等同于 [cleanForDelegate:nil]）
- (void)cleanWithDelegate:(id)delegate;		//清理对应代理的设备（停止扫描并移除未连接的设备，设备的delegate与传入相同或与控制器delegate相同时设为nil）

- (void)addServiceWithUUID:(CBUUID *)service;		//添加要支持扫描的服务

@end
